package cate.game.videohall.po;

import cate.common.table.d.GDVideoHall;
import cate.common.util.GameResult;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@NoteClass(value = "录像快照分组")
public class VideoGroupPO {
    @NoteField(value = "分组")
    public int group;

    @NoteField(value = "录像")
    public List<VideoPO> list;

    public VideoGroupPO() {

    }

    public VideoGroupPO(int group) {
        this.group = group;
        initialize();
    }

    public void initialize() {
        if (list == null) {
            list = new ArrayList<>();
        }
        removeExpired();

        // 删除多余的录像
        while (isOverage()) {
            removeOverage();
        }
    }

    /** 添加录像（满了就删） */
    public synchronized String addVideo(VideoPO video) {
        if (video == null) {
            return null;
        }
        if (getVideo(video.replayProfile.replayUid) != null) {
            return null;
        }
        return addToList(video);
    }

    private String addToList(VideoPO video){
        String replayUid = null;
        if (isFull()) {
            VideoPO old = removeOverage();
            if (old != null) {
                replayUid = old.replayProfile.replayUid;
            }
        }
        list.add(0,video);
        return replayUid;
    }

    private boolean isFull(){
        if(list.isEmpty()) {
            return false;
        }
        return list.size() >= GDVideoHall.getReplayMax(this.group);
    }

    private boolean isOverage(){
        if(list.isEmpty()) {
            return false;
        }
        return list.size() > GDVideoHall.getReplayMax(this.group);
    }

    public void removeExpired(){
        list.removeIf(a -> (a == null || a.replayProfile.isExpired(group)));
    }

    private VideoPO removeOverage(){
        if(!list.isEmpty()){
            return list.remove(list.size() - 1);
        }
        return null;
    }

    /** 收藏录像 */
    public GameResult<Void> collect(VideoPO video) {
        GameResult<Void> r = new GameResult<>();
        if (video == null) {
            return r.fail("录像为空");
        }
        if (getVideo(video.replayProfile.replayUid) != null) {
            return r.fail("录像已存在");
        }
        if (isFull()) {
            return r.fail("收藏已达上限");
        }
        list.add(video);
        return r.success();
    }

    public GameResult<Void> deleteVideo(String replayUid) {
        GameResult<Void> r = new GameResult<>();
        if (!list.removeIf(a -> a != null && StringUtils.equals(replayUid, a.replayProfile.replayUid))) {
            return r.fail("录像不存在");
        }
        return r.success();
    }

    public VideoPO getVideo(String replayUid) {
        for (VideoPO video : list) {
            if (video != null) {
                if (StringUtils.equals(video.replayProfile.replayUid, replayUid)) {
                    return video;
                }
            }
        }
        return null;
    }
}
